package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     *
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //创建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //复制到shoppingCart
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //查询自己购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());


        //判断商品是否在购物车中，通过数据层购物车里的商品数据作list对象
        List<ShoppingCart> shoppingCartList= shoppingCartMapper.list(shoppingCart);

        //判断购物车中是否有商品
        if (shoppingCartList != null&&shoppingCartList.size()==1){
            //存在
           shoppingCart=shoppingCartList.get(0);
           //存在更新数量加1
           shoppingCart.setNumber(shoppingCart.getNumber()+1);
           //更新数据层mapper数据
           shoppingCartMapper.updateNumberById(shoppingCart);
        }else{
            //不存在，就插入数据
            //判断数据时菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId !=null){
                //添加购物车的是菜品
                //菜品数据通过查询菜品Id
                Dish dish = dishMapper.getById(dishId);
                //购物车通过查询id来添加名字，图片，价格
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }else{
               //添加是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                //购物车通过查询id来添加名字，图片，价格
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //不存在，就插入数据，数量为1
            shoppingCart.setNumber(1);
            //创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //购物车数据层插入购物车
            shoppingCartMapper.insert(shoppingCart);


        }


    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        return shoppingCartMapper.list(shoppingCart);
    }

    public  void  cleanShoppingCart(){
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

}
